/* 
 * Converting a string into an integer
 * Suggested exercise:
 * - Change the for-loop to while-loop
 * - Print the position of the wrong input character
 */

import java.util.Scanner;

class str2int 
{

  public static void main (String[] args)
  {

    int val = 0, i;
    String num;
    char ch;
    boolean ok = true;

    Scanner scan = new Scanner(System.in);
    System.out.print ("Type an integer: ");
    num = scan.next(); //read a string
 
    for (i=0; (i<num.length()) & ok; i++) {
      ch = num.charAt(i);
      if (ch >= 48 & ch <= 57) 
        val = 10 * val + ch - 48;
      else 
        ok = false;
    }

    if (ok) System.out.println ("The value is: " + val);
       else System.out.println ("Incorrect number format");
 
  }
}
